xlv.obj.menu={
	extend:function(){
		this.extend('xlv.obj.ulli');
	},
	implement:function(){
	},
	create:function(tag){
		var div=document.createElement((tag||'div'));
		div.setAttribute('xlvobject','xlv.obj.menu');
		Element.addClassName(div,'class_menu');
		return div;			
	},
	properties:{
		type:'horz', //horz vert
		editable:false,
		clickTrigger:false,
		effect:true,
		dmName:null,
		dmPath:null,
		dynamicMenu:false
	},
	method:{
		initial:function(){
			this.$super('xlv.obj.ulli.initial');
			Element.addClassName(this,'class_menu');
			Element.addClassName(this,this.$P.type);
			this.addEventHandler('mouseover','_mouseover');
			this.addEventHandler('mouseout','_mouseout');
			this.addEventHandler('click','_click');
			if(this.$P.dynamicMenu){
			    this.$V._lastul=this.$V.rootUL;
				this.$V._timeout=setTimeout((function(){this._dismiss();}).bind(this), 1500);
			}
		},
		_dismiss:function(){
			var ul=this.$V._lastul;	
			while(ul && ul != this.$V.rootUL){
			    if(this.$P.effect)ul.style.display='none';
			    else ul.style.display='none';
				ul=xlv.util.findParent(ul,{tag:'ul'});
			}
			var menu=this;
			if(this.$P.dynamicMenu){
				if(this.$P.effect) ul.style.display='none';
				else this.parentNode.removeChild(menu);
			}
			this.$V._lastul=null;
			this.$V._clicked=false;
		},
		_runtimeStyle:function(ul){
			if(ul!=this.$V.rootUL){
				var lis=ul.getElementsByTagName('li');
				for(var i=0;i<lis.length; ++i){
					var childul=xlv.util.findDirectChild(lis[i],{tag:'ul'});
					if(childul) Element.addClassName(lis[i], 'class_menu_arrow');
				}
			}
		},
		_mouseover:function(e){
			var src=(e.srcElement||e.target);
			var p=this.$P,v=this.$V;
			if(p.clickTrigger && !v._clicked) return;
			if(v._timeout){clearTimeout(v._timeout);v._timeout=null;}
			var li=this.targetElement(src,{tag:'li'});
			var ul=this.targetElement(src,{tag:'ul'});
			if(v._lastul && xlv.util.findParent(v._lastul,{tag:'li'})==li) return;
			//
			while(v._lastul!=null && v._lastul!=ul){
				if(v._lastul==v.rootUL)break;
				if(p.effect)this._blockUp(v._lastul);
				else v._lastul.style.display='none';
				v._lastul=xlv.util.findParent(v._lastul,{tag:'ul'},{object:this});
			}
			var child_ul=(li)?xlv.util.findDirectChild(li,{tag:'ul'}):null;
			if(child_ul){
				if(this.$P.dmName && !child_ul._created){
					var indexes=xlv.util.indexesForLI(li);
					var dm=this.mvc_getModel();
					var node=dm.getNode(xlv.util.indexesToXPath(indexes),this._getRootNode(dm));
					this._createLIforChildren(dm,node,child_ul);
					child_ul._created=true;
				}
				this._runtimeStyle(child_ul);
				var level=xlv.util.levelOfDescendant(this,li,{tag:'ul'});
				var style={};
				var inline_1=(Element.getStyle(li,'display')=='inline'&&level==1);
				if(inline_1){
					style.top=li.offsetHeight+'px';
					style.left=li.offsetLeft+'px';
				}else {
					style.top=li.offsetTop+'px';
					style.left=(li.offsetParent.offsetWidth-4)+'px';
					if(v._lastul){
						var dim=Element.getDimensions(child_ul);
						var lt= Element.cumulativeOffset(v._lastul);
						var vlt=this.getDesktop().viewportOffset();
						if(lt[0]+v._lastul.offsetWidth+dim.width>vlt.width)style.left=(-1*dim.width+4)+'px';
					}
				}	
				v._lastul=child_ul;
				Element.setStyle(v._lastul,style);
				if(p.effect)this._blockDown(v._lastul);
				else v._lastul.style.display='block';
			}
		},
		_blockDown:function(ul){
			var effect=ul._effect;
			if(!effect){
				effect=ul._effect={extend:{element:ul},shrink:{element:ul,style:{height:0}}};
				var overflow=ul.style.overflow;
				Element.setStyle(ul,{display:'none',height:'','overflow':'hidden'});
				var dim=Element.getDimensions(ul);
				effect.extend.style={height:dim.height};
				effect.extend.finished=function(){
					Element.setStyle(effect.extend.element,{height:'',display:'block','overflow':overflow});
				};
				effect.shrink.finished=function(){
					Element.setStyle(effect.extend.element,{height:'',display:'none'});
				};
				Element.setStyle(ul,{height:'0px',display:'block'});
				xlv.util.styleEffect(effect.extend);
			}else{
				if(!effect.shrink.completed)effect.shrink.cancel=true;
				if(effect.extend.completed==true){
					effect.extend.cancel=false;
					effect.extend.completed=false;
					ul.style.overflow='hidden';
					xlv.util.styleEffect(effect.extend);
				}
			}
		},
		_blockUp:function(ul){
			var effect=ul._effect;
			if(effect){
				if(!effect.extend.completed)effect.extend.cancel=true;
				if(typeof effect.shrink.completed=='undefined'||effect.shrink.completed==true){
					effect.shrink.cancel=false;
					effect.shrink.completed=false;
					ul.style.overflow='hidden';
					xlv.util.styleEffect(effect.shrink);
				}
			}
		},
		_mouseout:function(e){
			//if(this.$V._timeout){clearTimeout(this.$V._timeout);this.$V._timeout=null;}
			this.$V._timeout=setTimeout((function(){this._dismiss();}).bind(this),300);
		},
		_click:function(event){
			var item=event.target;
			var p=this.$P,v=this.$V;
			var li=this.targetElement(item,{tag:'li'},{tag:'ul'});
			if(item!=this&&p.clickTrigger&&!v._clicked&&li){
				v._clicked=true;
				if(!v._lastul){
					this._mouseover({target:li});
					return;
				}
			}
			if(xlv.util.findDirectChild(li,{tag:'ul'})) return;
			this._dismiss(true);
			return;
		}
	}
};
